<template>
  <!--广告组、轮播图、组合组件、图片魔方中使用的内容设置，图片上传、链接选择-->
  <div class="hot_imgs borderPadding">
    <div class="title" v-if="configData.title || configData.tips">
      {{ configData.title || configData.tips }}
    </div>
    <div class="list-box mt20">
      <draggable class="dragArea list-group" :list="configData.list" group="peoples" handle=".move-icon">
        <div class="item" v-for="(item, index) in configData.list" :key="index">
          <div class="move-icon">
            <span class="iconfont icon-tuozhuaidian"></span>
          </div>
          <div class="img-box">
            <!-- <img :src="item.img" alt="" v-if="item.img" /> -->
            <!-- <div class="upload-box" v-else><i class="el-icon-camera-solid" style="font-size: 30px" /></div> -->
            <el-upload class="upload-box avatar-uploader" action="#" :show-file-list="false"
              :on-change="(file, fileList) => onUloadChange(file, fileList, item)" style="font-size: 30px">
              <img :src="item.img" alt="" v-if="item.img" />
              <i v-else class="el-icon-camera-solid"></i>
            </el-upload>
          </div>

          <div class="info">
            <div class="info-item" v-for="(infos, key) in item.info" :key="key">
              <span>{{ infos.title }}</span>
              <div class="input-box" @click="getLink(index, key, item.info)">
                <!-- <el-select v-if="!infos.select" v-model="infos.value" style="">
                  <el-option v-for="itm in linkList" :key="itm.value" :label="itm.label" :value="itm.value" />
                </el-select> -->
                <el-input size="small" v-model="infos.value" :readonly="key != 2" :placeholder="infos.tips"
                  :maxlength="infos.max">
                  <!-- <el-button v-if="key == item.info.length - 2" slot="append" icon="el-icon-arrow-right"></el-button> -->
                </el-input>
              </div>
            </div>
            <div class="delect-btn" @click.stop="bindDelete(item, index)" v-if="!configData.isCube">
              <i class="el-icon-error" style="font-size: 26px" />
            </div>
          </div>
        </div>
      </draggable>
    </div>
    <template v-if="configData.list">
      <div class="add-btn" v-if="configData.list.length < configData.maxList">
        <el-button class="button" icon="el-icon-plus" plain @click="addBox">添加版块</el-button>
      </div>
    </template>
    <!--  -->
    <typeMoviesList ref="typeMoviesListRef" :typeTableParams="configData.typeTableParams" @linkData="linkData">
    </typeMoviesList>
  </div>
</template>

<script>
// +---------------------------------------------------------------------
// 这个组件就是【大小图】的的右侧列表组件
// +---------------------------------------------------------------------
import vuedraggable from 'vuedraggable';
import typeMoviesList from '@/components/typeMoviesList';
import { fileToBase64 } from '@/utils/formate';
export default {
  name: 'c_menu_list_show',
  props: {
    configObj: {
      type: Object,
    },
    configNme: {
      type: String,
    },
    index: {
      type: null,
    },
    isRub: {
      type: Boolean,
      default: false,
    },
  },
  components: {
    draggable: vuedraggable,
    typeMoviesList,
  },
  data() {
    return {
      curTypeId: '',
      defaults: {},
      configData: {},
      gridBtn: {
        xl: 4,
        lg: 8,
        md: 8,
        sm: 8,
        xs: 8,
      },
      gridPic: {
        xl: 6,
        lg: 8,
        md: 12,
        sm: 12,
        xs: 12,
      },
      activeIndex: 0,
      indexLast: 0,
      lastObj: {},
      linkList: [],
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.defaults = this.configObj;
      this.configData = this.configObj[this.configNme];
    });
  },
  watch: {
    configObj: {
      handler(nVal, oVal) {
        this.defaults = nVal;
        this.configData = nVal[this.configNme];
        this.curTypeId = this.configData.typeId
        // console.log(11155111, );

      },
      deep: true,
    },
  },
  methods: {
    // 上传图片
    onUloadChange(file, _, item) {
      if (file.size >= 1024 * 1024 * 400) {
        this.$message.error("最大上传限制400MB！");
        return;
      }
      fileToBase64(file.raw).then(res => {
        item.img = res;
        // this.configData.list[this.activeIndex].img = res;
      })
    },
    //状态切换
    onchangeIsShow(e) {
      this.configData.list[this.activeIndex].status = e;
    },
    // 回显数据
    linkData(e) {
      this.configData.list[this.activeIndex].info[0].value = e.name;
      this.configData.list[this.activeIndex].info[this.indexLast - 1].value = '/pages/watchMovie/videos??id=' + e._id;
      this.configData.list[this.activeIndex].info[this.indexLast].video = e.video;
      this.configData.list[this.activeIndex].img = e.img
      this.configData.list[this.activeIndex].duration = e.duration
      this.configData.list[this.activeIndex].info[2].value = e.desc
      // console.log(11111, this.configData);
    },
    getLink(index, key, item) {
      console.log('typeTableParams',this.configData.typeTableParams);
      this.$forceUpdate();
      this.indexLast = item.length - 1;
      if (key == 2) {
        return;
      }
      this.activeIndex = index;
      if (Object.keys(this.configData.typeTableParams).length == 0) {
        this.$message.warning('请先选择电影类型')
        this.loding = false
        return
      }
      this.$refs.typeMoviesListRef.dialogVisible = true;
    },
    addBox() {
      if (this.configData.list.length == 0) {
        this.lastObj.img = '';
        this.lastObj.info[0].value = '';
        this.lastObj.info[1].value = '';
        this.configData.list.push(this.lastObj);
      } else {
        let obj = JSON.parse(JSON.stringify(this.configData.list[this.configData.list.length - 1]));
        obj.img = '';
        obj.info[0].value = '';
        obj.info[1].value = '';
        this.configData.list.push(obj);
      }
    },
    // 点击图文封面
    modalPicTap(item, index) {
      let _this = this;
      _this.$modalUpload(
        function (img) {
          if (!img) return;
          item.img = img[0].sattDir;
          if (_this.isRub) _this.getPic(img[0].sattDir);
        },
        null,
        'diy',
      );
    },
    // 获取图片信息
    getPic(pc) {
      this.$nextTick(() => {
        this.configData.list[this.activeIndex].img = pc;
        let data = this.defaults.menuConfig;
        if (data && data.isCube) {
          this.defaults.picStyle.picList.splice(this.defaults.picStyle.tabVal, 1, {
            image: pc,
            link: data.list[0].info[0].value,
          });
        }
      });
    },
    onBlur() {
      let data = this.defaults.menuConfig;
      this.defaults.picStyle.picList[this.defaults.picStyle.tabVal].link = data.list[0].info[0].value;
    },
    // 删除
    bindDelete(item, index) {
      if (this.configData.list.length == 1) {
        this.lastObj = this.configData.list[0];
      }
      this.configData.list.splice(index, 1);
    },
  },
};
</script>

<style scoped lang="scss">
.hot_imgs {
  margin-bottom: 20px;

  .title {
    color: #bbbbbb;
    font-size: 12px;
  }

  .list-box {
    .item {
      width: 100%;
      height: 140px;
      background: #f9f9f9;
      border-radius: 3px 3px 3px 3px;
      opacity: 1;
      position: relative;
      display: flex;
      justify-content: center;
      align-items: center;
      padding: 16px 20px;
      margin-bottom: 14px;

      .move-icon {
        display: flex;
        align-items: center;
        justify-content: center;
        height: 80px;
        cursor: move;
      }

      .img-box {
        position: relative;
        width: 64px;
        height: 64px;
        margin-left: 20px;

        img {
          width: 100%;
          height: 100%;
        }
      }
    }

    .info {
      flex: 1;
      margin-left: 22px;

      .info-item {
        display: flex;
        align-items: center;
        margin-bottom: 10px;

        span {
          width: 40px;
          font-size: 12px;
          color: #999999;
        }

        .input-box {
          flex: 1;

          ::v-deep.ivu-input {
            font-size: 13px !important;
          }
        }
      }
    }

    .delect-btn {
      position: absolute;
      right: -7px;
      top: -12px;

      .iconfont-diy,
      .iconfont {
        font-size: 25px;
        color: #999;
      }
    }
  }

  .add-btn .button {
    width: 100%;
    height: 36px;
    margin-top: 6px;
  }
}

.upload-box {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  width: 100%;
  height: 100%;
  background: #fff;
  font-size: 12px;
  color: #cccccc;
  border: 1px dashed #ccc;

  .iconfont {
    font-size: 16px;
  }
}

.iconfont-diy,
.iconfont {
  color: #dddddd;
  font-size: 28px;
}

.el-select {
  display: block;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 50px;
  height: 50;
  line-height: 50px;
  text-align: center;
}

.avatar {
  width: 50px;
  height: 50px;
  display: block;
}
</style>
